<template>
  <div class="contentIndex" id="contentIndex">
    <div>
    </div>
    <div class="contentIndexRight">
      <div class="right-headSelect">
        <baseTabs
          @changeTabs="changeTopTabs"
          :activeKey="contentType"
          :tabsList="tabsList"
        ></baseTabs>
        <div class="button-function">
          <div style="margin-left: 16px">
            <a-input
              @blur="searchTitle"
              @keyup.enter.native="searchTitle"
              v-model:value="title"
              allow-clear
              placeholder="搜索标题或作者"
              style="width: 192px"
            >
              <template #suffix>
                <i @click="searchTitle" class="iconfont icon-search"></i>
              </template>
            </a-input>
          </div>
          <div style="margin-left: 16px">
            <span style="font-size: 14px;">所属栏目：</span>
          <a-tree-select
          style="width: 192px"
            placeholder="请选择栏目"
            v-model:value="channelId"
            ref="usermanage_treeSelect"
            show-search
            :dropdown-style="{ maxHeight: '200px', overflow: 'auto' }"
            allow-clear
            treeNodeFilterProp="channelName"
            :fieldNames="{
              children: 'children',
              label: 'channelName',
              value: 'id',
            }"
            tree-default-expand-all
            :tree-data="gData"
            @change="onSelectType"
          >
          </a-tree-select>
        </div>
        <div style="margin-left: 16px">
          <span style="font-size: 14px;">创建时间：</span>
          <date-range-picker
          style="width: 350px"
            @change="changeTime"
            v-model:value="timeValue"
            :disabledInfo="{ type: 2, startDays: 0 }"
            :showTime="true"
            :valueFormat="valueFormat"
          ></date-range-picker>
        </div>
        </div>
      </div>
      <div class="contBox">
        <div
          class="contentIndexList"
          :class="'contentTable'"
          id="contentIndexList"
        >
          <quotedList
          :tabKey="contentType"
          :title="title"
          :channelId="channelId"
          :params="params"
          :specialId="specialId"
          ref="contentRef"
          >
          </quotedList>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import baseTabs from "@/components/tabs/baseTabs.vue";
import {
  ref,
  watch,
  onMounted,
  nextTick,
} from "vue";
import { dataSourceType } from "@/type";
import type { TreeProps } from "ant-design-vue";
import request from "@/api/index";
import quotedList from "@/c-views/colunm/specialTopic/quotedList.vue"
import getters from "@/store/getters";
import dateRangePicker from "@/components/picker/dateRangePicker.vue";
import { useRoute, useRouter } from "vue-router";
import { routerPerm } from "@/utils/common";
const btnPerm = ref<any[]>([]);
// route
const route = useRoute();
//data
const specialId = ref<any>(null)
const tabsList = [
  {
    tabName: "已引用",
    key: 1,
  },
  {
    tabName: "未引用",
    key: 2,
  },
];
const contentRef = ref<any>();
const title = ref<any>("");
const contentType = ref(1);
const searchValue = ref<string>("");
const expandedKeys = ref<any[]>([]);
const selectedKeys = ref<any[]>([]);
const defaultExpandAll = ref<Boolean>(true);
const autoExpandParent = ref<boolean>(true);
const gData = ref<any[]>([]);
const dataList: TreeProps["treeData"] = [];
const channelId = ref<any>(null);
const userInfo = getters.userInfo();
const valueFormat = ref("YYYY-MM-DD HH:mm:ss");
const timeValue = ref<(string | number)[]>([]);
const params = ref<dataSourceType>({
  createTimeStart: null,
  createTimeEnd: null
});
//method

//搜索
const searchTitle = () => {
  contentType.value === 1 ? contentRef.value.fetchCommentPage() : contentRef.value.fetchCommentReportPage()
};

// 搜索-栏目选择
const onSelectType = () => {
  nextTick(() => {
    contentType.value === 1 ? contentRef.value.fetchCommentPage() : contentRef.value.fetchCommentReportPage()
  })
};

// 筛选时间
const changeTime = (data: any) => {
  if (data) {
      params.value.createTimeStart = data[0];
      params.value.createTimeEnd = data[1];
  }else {
    params.value.createTimeStart = null;
      params.value.createTimeEnd = null;
  }
  nextTick(() => {
    contentType.value === 1 ? contentRef.value.fetchCommentPage() : contentRef.value.fetchCommentReportPage()
  })
};

//改变头部tab切换
const changeTopTabs = (activeKey: number) => {
  contentType.value = activeKey;
  title.value = ""
  channelId.value = null
  timeValue.value = []
  params.value.createTimeStart = null
  params.value.createTimeEnd = null
};
//获取栏目层级树列表
const fetchChannelListBySiteId = async () => {
  let data = await request.contentColumnTree({});
  nextTick(() => {
    gData.value = data;
    dataList.push(...data);
    getExpandKeyFunc();
  });
};

watch(searchValue, (value) => {
  parentKey.value = [];
  if (value) {
    dataList.map((item: any) => {
      getParentKey(value, item);
    });
  }
  expandedKeys.value = parentKey.value;
  searchValue.value = value;
  autoExpandParent.value = true;
});

const parentKey = ref<(string | number)[]>([]);
const getParentKey = (value: string | number, node: any) => {
  if (node.channelName.indexOf(value) > -1) {
    // parentKey = node.id;
    parentKey.value.push(node.id);
    if (node.children) {
      node.children.map((item: any) => {
        getParentKey(value, item);
      });
    }
  } else {
    if (node.children) {
      node.children.map((item: any) => {
        getParentKey(value, item);
      });
    }
  }
};


const traversal = (data: any) => {
  data.map((item: any) => {
    parentKey.value.push(item.id);
    if (item.children) {
      traversal(item.children);
    }
  });
  expandedKeys.value = parentKey.value;
};

const getExpandKeyFunc = () => {
  if (route.query.channelId) getParLoopKeys(gData.value, route.query.channelId);
  function getParLoopKeys(arr: any, id: any) {
    arr?.length &&
      arr.forEach((item: any) => {
        if (item.id == id) {
          expandedKeys.value.push(id);
          // if (item?.parentId) {
          //   expandedKeys.value.push(item.parentId)
          // }
        } else if (item?.children && item?.children.length) {
          getParLoopKeys(item.children, id);
        }
      });
  }
};

//onmounted
onMounted(async () => {
  specialId.value = route?.query?.id;
  btnPerm.value = routerPerm();
  fetchChannelListBySiteId();
});
</script>

<style lang="less" scoped>
.contentIndex {
  // background: #fff;
  // padding-bottom: 24px;
  height: calc(100% - 72px);
  display: flex;
  background-color: #fff;
  padding: 16px;

  .icon-search {
    color: rgba(0, 0, 0, 0.45);
    font-size: 20px !important;
    line-height: 20px;
  }

  .side-left {
    width: 200px;
    max-height: calc(100% - 24px);
    border-right: 1px solid rgba(0, 0, 0, 0.06);
    padding-right: 4px;
    position: relative;
    transition: width 0.3s;
    padding-right: 16px;

    .treeBox {
      height: 100%;
      width: 184px;
    }

    .treeTitle {
      margin-top: 16px;
      margin-bottom: 4px;
      font-size: 14px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 24px;
      border-radius: 2px;
      padding: 0 4px;
      margin-left: 24px;
      cursor: pointer;

      .allColumn {
        color: rgba(0, 0, 0, 0.85);
      }

      .iconfont {
        line-height: 24px;
        margin-left: 4px;
        color: rgba(0, 0, 0, 0.85);
      }

      .openorclose {
        cursor: pointer;
        display: flex;
        align-items: center;
      }
    }

    .openorclose:hover {
      color: #1f6ded;

      .iconfont {
        color: #1f6ded;
      }
    }

    .treeTitle:active {
      color: #1f6ded;

      .iconfont {
        color: #1f6ded;
      }
    }

    .treeTitle_no_background {
      background-color: transparent;
    }

    .treeTitle:hover {
      background-color: #f5f5f5;
    }

    .treeTitle_background {
      background-color: #edf7ff;

      &:hover {
        background-color: #edf7ff;
      }
    }

    ::v-deep(.ant-tree .ant-tree-node-content-wrapper.ant-tree-node-selected) {
      background-color: #edf7ff;
    }

    .open-right {
      position: absolute;
      top: 50%;
      right: -1px;
      margin-top: -15px;
      bottom: -20px;
      height: 30px;
      width: 20px;
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-content: center;
      background: #ffffff;
      border: 1px solid rgba(0, 0, 0, 0.06);
      z-index: 2;
    }

    .open-right-side {
      right: -4px;
    }

    .iconfont:hover {
      color: rgba(0, 0, 0, 0.6);
    }

    .iconfont {
      color: rgba(0, 0, 0, 0.45);
    }
  }

  .right-headSelect {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    border-bottom: 1px solid rgba(0, 0, 0, 0.06);
    margin: 0 16px;

    ::v-deep(
        .ant-tabs-top > .ant-tabs-nav::before,
        .ant-tabs-bottom > .ant-tabs-nav::before,
        .ant-tabs-top > div > .ant-tabs-nav::before,
        .ant-tabs-bottom > div > .ant-tabs-nav::before
      ) {
      border: none;
    }

    .button-function {
      height: 32px;
      display: flex;
      flex-direction: row;
      align-items: center;

      .addButton {
        display: flex;
        align-items: center;
        width: 108px;
        height: 32px;
        border-radius: 2px;

        .iconadd {
          font-size: 24px;
          margin-right: 4px;
        }
      }

      .open-option {
        margin-left: 8px;
      }

      .open-option:hover {
        border-color: #4892fa;
        color: #4892fa;
      }

      .open-option:active {
        border-color: #1050c7;
        color: #1050c7;
      }
    }

    .displaySwitch {
      margin-left: 16px;
      line-height: 20px;

      i {
        padding: 6px 8px;
        font-size: 20px;
        line-height: 20px;
        border: 1px solid #d9d9d9;
        color: rgba(0, 0, 0, 0.65);
        cursor: pointer;
      }

      i:first-child {
        border-radius: 2px 0px 0px 2px;
      }

      i:last-child {
        border-radius: 0px 2px 2px 0px;
      }

      .swithys {
        color: #1f6ded;
        background: #edf7ff;
        border: 1px solid #1f6ded;
      }
    }

    .importButton {
      margin-right: 8px;
    }
  }

  .select-content {
    background: rgba(0, 0, 0, 0.02);
    display: flex;
    margin: 16px 16px 4px 16px;
    align-items: center;
    flex-wrap: wrap;
    padding: 0 0 16px 16px;
    position: relative;

    .select-item {
      margin-right: 16px;
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-content: flex-end;
      margin-top: 16px;

      .title {
        font-size: 14px;
        font-family: PingFang SC-Regular, PingFang SC;
        font-weight: 400;
        width: 104px;
        margin-right: 12px;
        color: rgba(0, 0, 0, 0.85);
        text-align: right;
      }

      .title_right {
        flex: 1;
      }
    }

    .option-content {
      flex: 1;
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-content: flex-end;
      height: 32px;
      margin-top: 16px;
      margin-right: 16px;
      position: sticky;
      right: 16px;

      .reset-btn {
        height: 32px;
      }

      .reset-btn:hover {
        border-color: #4892fa;
        color: #4892fa;
      }

      .reset-btn:active {
        border-color: #1050c7;
        color: #1050c7;
      }

      .openorclose {
        display: flex;
        flex-direction: row;
        align-items: center;
        margin-left: 16px;
        font-size: 14px;
        font-family: PingFang SC-Regular, PingFang SC;
        font-weight: 400;
        color: #1f6ded;

        .iconfont {
          margin-left: 4px;
        }
      }
    }
  }

  .side-close {
    width: 0;
    border: none;
    transition: width 0.3s;

    .treeBox {
      display: none;
    }
  }
}

.contentIndexList {
  background-color: #fff;
  width: 100%;
  height: 100%;
  flex: 1;
  flex-direction: column;
}

.contentTable {
  // overflow: auto;
}

.contentIndexLeft {
  padding-right: 24px;
  width: 216px;
  height: 100%;
}

.contentIndexRight {
  flex: 1;
  width: 0; // 不要删除，不然flex:1不生效
  // display: flex;
  // flex-direction: column;

  .ant-tabs {
    padding-right: 16px;
    // height: 20px;
    background-color: #fff;
  }

  .contBox {
    position: relative;
    height: calc(100% - 40px);
  }

  .boxHeight {
    height: calc(100% - 200px);
  }
}

.new-add-box {
  background: #ffffff;
  box-shadow: 0px 9px 28px 8px rgba(0, 0, 0, 0.05),
    0px 6px 16px 0px rgba(0, 0, 0, 0.08), 0px 3px 6px -4px rgba(0, 0, 0, 0.12);
  border-radius: 2px;
  padding: 20px;

  .add-title {
    font-size: 14px;
    line-height: 14px;
    display: block;
    color: #000000d9;
  }

  .add-list {
    margin-top: 16px;
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;

    .add-item {
      margin-right: 8px;
      padding: 3px 12px;
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-content: space-between;
      border-radius: 2px;
      border: 1px solid rgba(0, 0, 0, 0.15);

      .icon-Add {
        display: block;
        line-height: 24px;
        font-size: 24px;
        color: #000000d9;
      }

      span {
        font-size: 14px;
        color: #000000d9;
      }
    }

    .add-item:hover {
      cursor: pointer;
    }

    .add-item:hover {
      border-color: #1f6dedff;

      .icon-Add {
        color: #1f6dedff;
      }

      span {
        color: #1f6dedff;
      }
    }

    .add-item:active {
      border-color: #1050c7ff;

      .icon-Add {
        color: #1050c7ff;
      }

      span {
        color: #1050c7ff;
      }
    }
  }
}

::v-deep(.ant-tabs-tab + .ant-tabs-tab) {
  margin: 0 0 0 24px;
}

::v-deep(.ant-tabs-tab) {
  padding: 8px 0;
}

::v-deep(::-webkit-scrollbar) {
  height: 10px;
}

::v-deep(.side-left .ant-tree-list-holder) {
  width: auto;
  height: 100%;
  transition: width 0.3s;
}

::v-deep(.side-close .ant-tree-list-holder) {
  width: 0;
  height: 100%;
  transition: width 0.3s;
}

::v-deep(.side-left .ant-tree-list:hover .ant-tree-list-scrollbar) {
  display: block !important;
}

::v-deep(.ant-tree-list .ant-tree-list-scrollbar) {
  display: none !important;
}

::v-deep(.ant-tree-list-scrollbar-thumb) {
  background: rgba(0, 0, 0, 0.15) !important;
}

::v-deep(.ant-checkbox + span) {
  padding-left: 12px;
  padding-right: 0;
}

::v-deep(.ant-table-wrapper) {
  margin-top: 0px;
}

::v-deep(.ant-table-thead > tr > th) {
  // padding: 16px 0 16px 16px;
}
</style>
<style lang="less">
.side-left {
  .ant-tree {
    height: calc(100% - 88px);
    overflow: auto;

    .ant-tree-treenode {
      width: 100%;

      .ant-tree-node-content-wrapper {
        width: 100%;
      }
    }

    .ant-tree::-webkit-scrollbar {
      display: none;
    }

    .ant-tree:hover::-webkit-scrollbar {
      display: block;
    }

    .ant-tree-list-scrollbar-thumb {
      height: auto !important;
    }

    .ant-tree-list-holder {
      overflow-y: unset !important;
    }
  }
}

@media screen and (min-width: 1800px) {
  .select-content {
    .select-item {
      width: calc(32.99999% - 12px);
    }

    .select-item:nth-of-type(3) {
      margin-right: 0;
    }
  }
}

@media screen and (max-width: 1780px) {
  .select-content {
    .select-item {
      width: calc(50% - 16px);
    }

    .select-item:nth-of-type(2n) {
      margin-right: 0;
    }
  }
}
</style>
